package com.lemon.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lemon.domain.PageResult;
import com.lemon.utils.UserThreadLocal;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lemon.mapper.StocktakingTaskMapper;
import com.lemon.domain.StocktakingTask;
import com.lemon.service.StocktakingTaskService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class StocktakingTaskServiceImpl extends ServiceImpl<StocktakingTaskMapper, StocktakingTask> implements StocktakingTaskService{

    @Autowired
    private StocktakingTaskMapper stocktakingTaskMapper;

    @Autowired
    private TaskSchedulerService taskSchedulerService;

    /**
     * 手动执行一次定时任务
     * @param taskId 任务ID
     */
    @Override
    public void executeTaskOnce(Long taskId) {
        // 查询任务详情
        StocktakingTask task = stocktakingTaskMapper.selectById(taskId);
        if (task == null) {
            throw new RuntimeException("任务不存在，ID: " + taskId);
        }

        // 执行任务
        taskSchedulerService.executeStocktakingTask(task);
    }

    /**
     * 添加定时盘点任务
     */
    @Override
    @Transactional
    public void addStocktakingTask(StocktakingTask task) {
        // 设置创建时间
        task.setCreateTime(LocalDateTime.now());
        task.setEmpId(UserThreadLocal.get().getId());
        task.setIsEnable(2);
        // 保存到数据库
        stocktakingTaskMapper.insert(task);

        // 如果任务是启用状态，则注册定时任务
        if (task.getIsEnable() == 1) {
            taskSchedulerService.scheduleTask(task);
        }
    }

    /**
     * 更新定时任务
     */
    @Override
    @Transactional
    public void updateStocktakingTask(StocktakingTask task) {
        StocktakingTask stocktakingTask = stocktakingTaskMapper.selectById(task.getId());
        stocktakingTask.setStartTime(LocalDateTime.now());
        stocktakingTask.setIsEnable(task.getIsEnable());

        // 更新数据库
        stocktakingTaskMapper.updateById(stocktakingTask);

        // 重新调度任务
        taskSchedulerService.rescheduleTask(stocktakingTask);
    }

    /**
     * 删除定时任务
     */
    @Override
    @Transactional
    public void deleteStocktakingTask(Long taskId) {
        stocktakingTaskMapper.deleteById(taskId);
        // 取消调度
        taskSchedulerService.cancelTask(taskId);
    }

    @Override
    public PageResult getPage(Integer page, Integer pageSize, String taskName, Integer category, Integer isEnable) {
        PageHelper.startPage(page,pageSize);
        List<StocktakingTask> stocktakingTasks = stocktakingTaskMapper.selectList(Wrappers.<StocktakingTask>lambdaQuery().like(ObjUtil.isNotEmpty(taskName), StocktakingTask::getTaskName, taskName).eq(ObjUtil.isNotEmpty(category), StocktakingTask::getCategory, category).eq(ObjUtil.isNotEmpty(isEnable), StocktakingTask::getIsEnable, isEnable));
        Page list = (Page) stocktakingTasks;
        return new PageResult(list.getTotal(), list.getResult());
    }
}
